<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>用户后台</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <link rel="stylesheet" href="/layui/css/admin.css">
    <script src="https://cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
    <link rel="stylesheet" href="/editormd/css/editormd.css" />

    <script src="/editormd/editormd.min.js"></script>
</head>
<body class="layui-layout-body">
<div class="layui-layout layui-layout-admin">
    <div class="layui-header">
        <div class="layui-logo">后台管理</div>
        <!-- 头部区域（可配合layui已有的水平导航） -->

        <ul class="layui-nav layui-layout-right">
            <li class="layui-nav-item">
                <a href="javascript:;">
                    <img src="/layui/images/1.jpg" class="layui-nav-img">
                    <%= user.username %>
                </a>
                <dl class="layui-nav-child">
                    <dd><a href="">基本资料</a></dd>
                    <dd><a href="">安全设置</a></dd>
                </dl>
            </li>
            <li class="layui-nav-item"><a href="/user/<%= user.username %>">回到主页</a></li>
        </ul>
    </div>

    <div class="layui-side layui-bg-black">
        <div class="layui-side-scroll">
            <!-- 左侧导航区域（可配合layui已有的垂直导航） -->
            <ul class="layui-nav layui-nav-tree" lay-filter="test">
                <li class="layui-nav-item layui-nav-itemed">
                    <a class="" href="javascript:;">文章</a>
                    <dl class="layui-nav-child">
                        <dd class="layui-this"><a href="/admin/task/addtask">文章发布</a></dd>
                        <dd><a href="/admin/task/all">文章管理</a></dd>
                    </dl>
                </li>

                <% if(user.level >= 10) { %>
                    <li class="layui-nav-item">
                        <a href="/admin/user">用户管理</a>
                    </li>
                <% } %>
            </ul>
        </div>
    </div>

    <div class="layui-body">
        <!-- 内容主体区域 -->
        <div style="padding: 15px;">
            <fieldset class="layui-elem-field">
                <legend>发布</legend>
            </fieldset>
            <form class="layui-form" action="">
                <div class="layui-form-item">
                    <label class="layui-form-label">文章标题</label>
                    <div class="layui-input-inline">
                        <input type="text" name="title" required lay-verify="required" placeholder="请输入标题"
                               autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">所属目录</label>
                    <div class="layui-input-inline">
                        <select name="dir" lay-verify="required">
                            <option value=""></option>
                            <option value="日记">日记</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">文章类别</label>
                    <div class="layui-input-block">
                        <input type="radio" name="class" value="日记" title="日记" checked>
                        <input type="radio" name="class" value="图片" title="图片">
                        <input type="radio" name="class" value="名言" title="名言">
                        <input type="radio" name="class" value="视频" title="视频">
                        <input type="radio" name="class" value="链接" title="链接">
                    </div>
                </div>
                <div id="test-editor">
                    <textarea style="display:none;"></textarea>
                </div>
<!--                <div class="layui-form-item layui-form-text">-->
<!--                    <label class="layui-form-label">内容</label>-->
<!--                    <div class="layui-input-block">-->
<!--                        <textarea id="demo" style="display: none;"></textarea>-->
<!--                    </div>-->
<!--                </div>-->
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="form">立即提交</button>
                    </div>
                </div>

            </form>
        </div>
    </div>

    <div class="layui-footer">
        <!-- 底部固定区域 -->
        公告 - 底部固定区域
    </div>
</div>
<script src="/layui/layui.js"></script>
<script>
        var editor = editormd("test-editor", {
            width  : "100%",
            height : "1000px",
            path   : "/editormd/lib/",
            syncScrolling : "single",
            emoji : true,
            saveHTMLToTextarea : true,
            tocm : true, // Using [TOCM]
            tex : true, // 开启科学公式TeX语言支持，默认关闭
            flowChart : true, // 开启流程图支持，默认关闭
            htmlDecode : true, // Decode all html tags & attributes
            // Filter tags/attributes expression : tagName,tagName,...|attrName,attrName,...
            htmlDecode : "style,script,iframe,sub,sup|on*"  // Filter tags, and all on* attributes
            //htmlDecode : "style,script,iframe,sub,sup|*"    // Filter tags, and all attributes
            //htmlDecode : "style,script,iframe,sub,sup,embed|onclick,title,onmouseover,onmouseout,style" // Filter tags, and your custom attributes
        });
    //JavaScript代码区域
    layui.use(['element', 'table', 'form', 'rate', 'laydate', 'layedit', 'layer'], function () {
        var element = layui.element;
        var form = layui.form;
        var layer = layui.layer;
        var layedit = layui.layedit;
        var $ = layui.$;

        var edit = layedit.build('demo', {
            height: 500,
            uploadImage: {
                url: '/api/upload' //接口url
                , type: 'post' //默认post
            }
        });
        form.on('submit(form)', (data) => {
            let da = {};
            da.title = data.field.title;
            da.dir = data.field.dir;
            da.class = data.field.class;
            // da.content = layedit.getContent(edit);
            da.content = editor.getHTML();
            let date = new Date();
            da.time = date;
            console.log(da);
            $.ajax({
                url: '/admin/task/add',
                type: 'post',
                data: da,
                success: function (res) {
                    if (res.code == 1) {
                        layer.alert(res.msg)
                    } else if (res.code == 0) {
                        layer.alert(res.msg, () => {
                            window.location.href = '/admin/task/all';
                        })
                    } else {
                        layer.alert('登录已过期，请先登录', () => {
                            window.location.href = '/login';
                        });
                    }
                }
            })
            // return false;
        })
    });
</script>
</body>
</html>

